
package com.swak.frame.system;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

import com.swak.frame.common.support.PropertiesFactory;
import com.swak.frame.exception.SwakAssert;
import com.swak.frame.util.GetterUtil;

/**
 * SystemConfigurable ClassName: SystemConfigurable.java
 * 
 * @author colley.ma
 * @date 2021年3月24日 下午12:03:26
 */
public class SystemConfigurable implements EnvironmentAware, InitializingBean {

    private String runZone;

    private String runAlias;

    private String bizName;
    private boolean runJob = false;
    private String mqType;

    private String appName;

    private String jdosAppName;

    private SysEnv sysEnv;

    protected transient Environment environment;

    public String getAppName() {
        return appName;
    }

    public void setRunZone(String runZone) {
        this.runZone = runZone;
    }

    public void setRunAlias(String runAlias) {
        this.runAlias = runAlias;
    }

    public void setBizName(String bizName) {
        this.bizName = bizName;
    }

    public void setRunJob(boolean runJob) {
        this.runJob = runJob;
    }

    public void setMqType(String mqType) {
        this.mqType = mqType;
    }

    public String getRunZone() {
        return runZone;
    }

    public String getRunAlias() {
        return runAlias;
    }

    public String getBizName() {
        return bizName;
    }

    public boolean isRunJob() {
        return runJob;
    }

    public String getMqType() {
        return mqType;
    }

    public boolean isTestEnv() {
        return SysEnv.TEST.equals(sysEnv);
    }

    public String getEnv() {
        return sysEnv.env;
    }

    public boolean isStgEnv() {
        return SysEnv.STG.equals(sysEnv);
    }

    public boolean isPref() {
        return SysEnv.PREF.equals(sysEnv);
    }

    public boolean isProdAndTestEnv() {
        if (isTestEnv() || isProductEnv()) {
            return true;
        }
        return false;
    }

    public boolean isProductEnv() {
        return SysEnv.PROD.equals(sysEnv);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = (ConfigurableEnvironment) environment;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        /**
         * 首先人工配置，如果人工配置的为空，从环境变量中获取
         */
        this.runZone = GetterUtil.getString(this.runZone,
            environment.getProperty(SystemConstant.SYS_RUN_ZONE, "LF"));
        this.runAlias = GetterUtil.getString(this.runAlias,
            environment.getProperty(SystemConstant.SYS_RUN_ALIAS));
        this.bizName = GetterUtil.getString(this.bizName,
            environment.getProperty(SystemConstant.SYS_BIZ_NAME));
        this.mqType = GetterUtil.getString(this.mqType,
            environment.getProperty(SystemConstant.SYS_MQ_TYPE, "consumer"));
        if (StringUtils.isNotEmpty(environment.getProperty(SystemConstant.SYS_RUN_JOB))) {
            if (GetterUtil.getBoolean(environment.getProperty(SystemConstant.SYS_RUN_JOB))
                && !runJob) {
                this.runJob = true;
            }
        }

        this.jdosAppName = GetterUtil.getString(this.jdosAppName,
            environment.getProperty(SystemConstant.SYS_APP_NAME));
        if (StringUtils.isEmpty(this.jdosAppName)) {
            this.jdosAppName = System.getenv("deploy_app_name");
            if (StringUtils.isNotEmpty(this.jdosAppName) && !this.jdosAppName.startsWith("jdos_")) {
                this.jdosAppName = "jdos_" + this.jdosAppName;
            }
        }
        if (StringUtils.isNotEmpty(this.jdosAppName) && this.jdosAppName.startsWith("jdos_")) {
            this.appName = this.jdosAppName.substring(5);
        } else {
            this.appName = this.jdosAppName;
        }
        this.sysEnv = SysEnv.getEnv(environment.getProperty(SystemConstant.SYS_ENV_KEY));

        SwakAssert.notEmpty(this.bizName,
            "[swak-Assert] - this bizName value must not be empty;shold be config in application.properties 'swak.system.bizName=xxx'");
        SwakAssert.notEmpty(this.appName,
            "[swak-Assert] - this appName value must not be empty;shold be config in application.properties 'swak.system.appName=xxx'");
        SwakAssert.notNull(this.sysEnv,
            "[swak-Assert] - this sysEnv value must not be empty;shold be config in application.properties 'swak.system.env=test or config in system.properties'");
    }

    public String getJdosAppName() {
        return jdosAppName;
    }

    public void setJdosAppName(String jdosAppName) {
        this.jdosAppName = jdosAppName;
    }

    public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
        return environment.getProperty(key, targetType, defaultValue);
    }

    public String getString(String key, String defaultVal) {
        return environment.getProperty(key, String.class,
            PropertiesFactory.getString(key, defaultVal));
    }

    public String getString(String key) {
        return environment.getProperty(key, String.class, PropertiesFactory.getString(key));
    }

    public boolean getBoolean(String key, boolean defaultVal) {
        return environment.getProperty(key, Boolean.class,
            PropertiesFactory.getBoolean(key, defaultVal));
    }

    public Integer getInteger(String key, int defaultValue) {
        return environment.getProperty(key, Integer.class,
            PropertiesFactory.getInt(key, defaultValue));
    }

    public Integer getInteger(String key) {
        return environment.getProperty(key, Integer.class, PropertiesFactory.getInt(key));
    }

    public Long getLong(String key, long defaultValue) {
        return environment.getProperty(key, Long.class,
            PropertiesFactory.getLong(key, defaultValue));
    }

    public Long getLong(String key) {
        return environment.getProperty(key, Long.class, PropertiesFactory.getLong(key));
    }

    public Environment getEnvironment() {
        return environment;
    }
}
